package servlet;


import java.io.IOException;
import java.io.PrintWriter;

import data.User;
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import listener.UserSessionListener;
import service.UserService;

@SuppressWarnings("serial")
public class UserServlet extends HttpServlet
{
	/**
	 * get与post的参数名
	 * 一般在getParameter方法中使用
	 * */
	public enum ArgName
	{
		GET_GET("get"),
		
		POST_TYPE("type"),
		POST_USERNAME("username"),
		POST_PASSWORD("password");
		
		String name;
		
		private ArgName(String name)
		{
			this.name = name;
		}
		
		public String getName()
		{
			return this.name;
		}
	}
	
	
	/**
	 * get的参数值
	 * 一般会用到getValue()
	 * */
	public enum GetValue
	{
		STATE_LOGIN,//get
		ONLINE_COUNT;//get
		
		public String getValue()
		{
			return this.toString();
		}
	}
	
	
	/**
	 * post的参数值
	 * 一般会用到getValue()
	 * */
	public enum PostValue
	{
		REGISTER,//type
		LOGIN,//type
		LOGOUT;//type
		
		public String getValue()
		{
			return this.toString();
		}
	}
	
	private static UserService service_user = new UserService();
	
	
	@Override
	public void init(ServletConfig config) throws ServletException
	{
		super.init(config);
		System.out.println(this.toString() + ".init");
	}
	
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException
	{
		System.out.println(this.toString() + ".doGet");
		String arg_get = request.getParameter(UserServlet.ArgName.GET_GET.getName());
		
		if (arg_get == null)
			return;
		
		PrintWriter out = response.getWriter();
		response.setContentType("text/plain;charset=UTF-8");
		
		if (arg_get.equals(UserServlet.GetValue.STATE_LOGIN.getValue()))
		{
			HttpSession session = request.getSession();
			String username = (String)session.getAttribute(User.Attribute.USERNAME.getFullName());
			String password = (String)session.getAttribute(User.Attribute.PASSWORD.getFullName());
			
			if (username != null && password != null)
			{
				UserService.ReturnStatus status = UserServlet.service_user.login(username, password);
				if (status == UserService.ReturnStatus.SUCCESS)
					out.print(true);
				else
					out.print(false);
			}
			else
				out.print(false);
		}
		else if (arg_get.equals(UserServlet.GetValue.ONLINE_COUNT.getValue()))
		{
			int count = UserSessionListener.getOnlineUserCount();
			System.out.println("在线人数：" + count);
			out.println(count);
		}
		else
		{
			//out.println("传参错误");
			out.println("error parameter");
		}
		
		out.flush();
		out.close();
	}
	
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException
	{
		System.out.println(this.toString() + ".doPost");
		String arg_type = request.getParameter(UserServlet.ArgName.POST_TYPE.getName());
		
		if (arg_type == null)
			return;
		
		PrintWriter out = response.getWriter();
		String arg_username = request.getParameter(UserServlet.ArgName.POST_USERNAME.getName());
		String arg_password = request.getParameter(UserServlet.ArgName.POST_PASSWORD.getName());
		
		//response.setContentType("text/html;charset=UTF-8");
		response.setContentType("text/plain;charset=UTF-8");
		
		if(arg_type.equals(UserServlet.PostValue.LOGOUT.getValue()))
		{
			HttpSession session = request.getSession();
			Integer sa_id = (Integer)session.getAttribute(User.Attribute.ID.getFullName());
			String sa_username = (String)session.getAttribute(User.Attribute.USERNAME.getFullName());
			String sa_password = (String)session.getAttribute(User.Attribute.PASSWORD.getFullName());
			
			if (sa_id != null
				&& sa_username != null
				&& !sa_username.equals("")
				&& sa_password != null
				&& !sa_password.equals(""))
			{
				//out.println("用户登出");
				out.println("user logout");

				session.removeAttribute(User.Attribute.ID.getFullName());
				session.removeAttribute(User.Attribute.USERNAME.getFullName());
				session.removeAttribute(User.Attribute.PASSWORD.getFullName());
			}
			else
			{
				//out.println("尚未登录");
				out.println("you have not logined");
			}
			
			out.flush();
			out.close();
			return;
		}
		
		
		if (arg_username == null || arg_username.equals(""))
		{
			//out.println("请输入用户名");
			out.println("input your username please");
			out.flush();
			out.close();
			return;
		}
		else if(arg_password == null || arg_password.equals(""))
		{
			//out.println("请输入密码");
			out.println("input your password please");
			out.flush();
			out.close();
			return;
		}
		
		if (arg_type.equals(UserServlet.PostValue.REGISTER.getValue()))
		{
			UserService.ReturnStatus status = UserServlet.service_user.register(arg_username, arg_password);
			if (status == UserService.ReturnStatus.EXIST_USER)
			{
				//out.println("用户名已存在");
				out.println("this username have used");
			}
			else if (status == UserService.ReturnStatus.SUCCESS)
			{
				//out.println("注册成功");
				out.println("register success");
			}
			else
			{
				//out.println("注册失败");
				out.println("register failed");
			}
		}
		else if (arg_type.equals(UserServlet.PostValue.LOGIN.getValue()))
		{
			UserService.ReturnStatus status = UserServlet.service_user.login(arg_username, arg_password);
			if (status == UserService.ReturnStatus.NOT_EXIST_USER)
			{
				//out.println("用户不存在");
				out.println("this user is not exist");
				out.flush();
				out.close();
				return;
			}
			else if (status == UserService.ReturnStatus.FAILED)
			{
				//out.println("密码错误");
				out.println("error password");
			}
			else
			{
				//out.println("登录成功");
				out.println("login success");
				HttpSession session = request.getSession();
				
				session.setAttribute(User.Attribute.USERNAME.getFullName(), arg_username);
				session.setAttribute(User.Attribute.PASSWORD.getFullName(), arg_password);
			}
		}
		else
		{
			//out.println("传参错误");
			out.println("error parameter");
		}
		
		
		out.flush();
		out.close();
	}
	
	
}








